from commonTool.logs.logs_wrapper import LogUtil

logs = LogUtil()


class api_response:
    resp = {
        "code": 0,
        "msg": "",
        "data": ""
    }

    @logs
    def new_success(self, **kwargs):
        msg = kwargs.get("msg")
        resp_data = kwargs.get("data")
        self.resp["code"] = 0
        self.resp["msg"] = msg if msg is not None and msg != "" else "操作成功"
        self.resp["data"] = resp_data if resp_data is not None else ""
        logs.info(self.resp)
        return self.resp

    @logs
    def new_fail(self, **kwargs):
        code = kwargs.get("code")
        if not code:
            raise RuntimeError("please key in code")
        msg = kwargs.get("msg")
        resp_data = kwargs.get("data")
        self.resp["code"] = code
        self.resp["msg"] = msg if msg is not None and msg != "" else "操作失败"
        self.resp["data"] = resp_data if resp_data is not None else ""
        logs.error(self.resp)
        print(self.resp)
        return self.resp
